package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.entity.AirLaneEntity;
import com.example.entity.SpatialGridEntity;
import com.example.entity.TakeoffLandEntity;
import com.example.entity.WorkZoneEntity;
import com.example.mapper.AirLaneMapper;
import com.example.mapper.SpatialGridMapper;
import com.example.mapper.TakeoffLandMapper;
import com.example.mapper.WorkZoneMapper;
import com.example.util.MicroResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service
public class SafeIntervalAndCapacityAccessService {

    /////////////////////////   航路AirLane的增删改查、分页  下  //////////////////////////////////////
    @Autowired
    private AirLaneMapper airLaneMapper;
    // 查 全部
    public List<AirLaneEntity> findAllAirLanes() {
        return airLaneMapper.selectList(null);
    }
    // 查 某id
    public Optional<AirLaneEntity> findAirLaneById(Long id) {
        return Optional.ofNullable(airLaneMapper.selectById(id));
    }
    // 增
    public AirLaneEntity saveAirLane(AirLaneEntity user) {
        airLaneMapper.insert(user);
        return user;
    }
    // 改
    public Optional<AirLaneEntity> updateAirLane(AirLaneEntity user) {
        int i = airLaneMapper.updateById(user);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }
    // 删
    public boolean deleteAirLane(Long id) {
        if (airLaneMapper.selectById(id) != null) {
            airLaneMapper.deleteById(id);
            return true;
        }
        return false;
    }

    // 安全间隔字符串数据处理
    public String SafeIntervalDecode(String SafeIntervalRepo)
    {

        return "123";
    }

    // 航路区条件查询
    public ResponseEntity<List<AirLaneEntity>> getAirlaneCondition(String index,Integer type)
    {
        QueryWrapper<AirLaneEntity> queryWrapper = new QueryWrapper<>();

        if ( index != null && !index.isEmpty()   &&   type != null ) {
            queryWrapper.like("region_index", index);
            queryWrapper.like("region_type", type);
        }
        else if (type != null) {
            queryWrapper.like("region_type", type);
        }
        else if (index != null && !index.isEmpty()) {
            queryWrapper.like("region_index", index);
        }

        List<AirLaneEntity> airlaneCondition = airLaneMapper.selectList(queryWrapper);
        ResponseEntity responseEntity;
        if (!CollectionUtils.isEmpty(airlaneCondition)) {
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(airlaneCondition));
            return responseEntity;
        } else {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }
    /////////////////////////   航路AirLane的增删改查、分页  上  //////////////////////////////////////


    ///////////////////////// 空域网格Spatial Grid的增删改查、分页  下 //////////////////////////////////////
    @Autowired
    private SpatialGridMapper spatialGridMapper;
    // 查 全部
    public List<SpatialGridEntity> findAllSpatialGrids() {
        return spatialGridMapper.selectList(null);
    }
    // 查 某id
    public Optional<SpatialGridEntity> findSpatialGridsById(Long id) {
        return Optional.ofNullable(spatialGridMapper.selectById(id));
    }
    // 增
    public SpatialGridEntity saveSpatialGrids(SpatialGridEntity user) {
        spatialGridMapper.insert(user);
        return user;
    }
    // 改
    public Optional<SpatialGridEntity> updateSpatialGrids(SpatialGridEntity user) {
        int i = spatialGridMapper.updateById(user);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }
    // 删
    public boolean deleteSpatialGrids(Long id) {
        if (spatialGridMapper.selectById(id) != null) {
            spatialGridMapper.deleteById(id);
            return true;
        }
        return false;
    }

    ///////////////////////// 空域网格Spatial Grid的增删改查、分页  上 //////////////////////////////////////


    ///////////////////////// 起降区Takeoff land的增删改查、分页  下 //////////////////////////////////////
    @Autowired
    private TakeoffLandMapper takeoffLandMapper;
    // 查 全部
    public List<TakeoffLandEntity> findAllTakeoffLand() {
        return takeoffLandMapper.selectList(null);
    }
    // 查 某id
    public Optional<TakeoffLandEntity> findTakeoffLandById(Long id) {
        return Optional.ofNullable(takeoffLandMapper.selectById(id));
    }
    // 增
    public TakeoffLandEntity saveTakeoffLand(TakeoffLandEntity user) {
        takeoffLandMapper.insert(user);
        return user;
    }
    // 改
    public Optional<TakeoffLandEntity> updateTakeoffLand(TakeoffLandEntity user) {
        int i = takeoffLandMapper.updateById(user);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }
    // 删
    public boolean deleteTakeoffLand(Long id) {
        if (takeoffLandMapper.selectById(id) != null) {
            takeoffLandMapper.deleteById(id);
            return true;
        }
        return false;
    }

    // 起降区区条件查询
    public ResponseEntity<List<TakeoffLandEntity>> getTakeofflandCondition(String index,Integer type)
    {
        QueryWrapper<TakeoffLandEntity> queryWrapper = new QueryWrapper<>();

        if ( index != null && !index.isEmpty()   &&   type != null ) {
            queryWrapper.like("region_index", index);
            queryWrapper.like("region_type", type);
        }
        else if (type != null) {
            queryWrapper.like("region_type", type);
        }
        else if (index != null && !index.isEmpty()) {
            queryWrapper.like("region_index", index);
        }

        List<TakeoffLandEntity> takeofflandCondition = takeoffLandMapper.selectList(queryWrapper);
        ResponseEntity responseEntity;
        if (!CollectionUtils.isEmpty(takeofflandCondition)) {
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(takeofflandCondition));
            return responseEntity;
        } else {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }
    ///////////////////////// 起降区Takeoff land的增删改查、分页  上 //////////////////////////////////////


    ///////////////////////// 工作区Work Zone的增删改查、分页  下 //////////////////////////////////////
    @Autowired
    private WorkZoneMapper workZoneMapper;
    // 查 全部
    public List<WorkZoneEntity> findAllWorkZone() {
        return workZoneMapper.selectList(null);
    }
    // 查 某id
    public Optional<WorkZoneEntity> findWorkZoneById(Long id) {
        return Optional.ofNullable(workZoneMapper.selectById(id));
    }
    // 增
    public WorkZoneEntity saveWorkZone(WorkZoneEntity user) {
        workZoneMapper.insert(user);
        return user;
    }
    // 改
    public Optional<WorkZoneEntity> updateWorkZone(WorkZoneEntity user) {
        int i = workZoneMapper.updateById(user);
        if(i == 1){
            return Optional.of(user);
        }
        return Optional.empty();
    }
    // 删
    public boolean deleteWorkZone(Long id) {
        if (workZoneMapper.selectById(id) != null) {
            workZoneMapper.deleteById(id);
            return true;
        }
        return false;
    }

    // 工作区区条件查询
    public ResponseEntity<List<WorkZoneEntity>> getWorkzoneCondition(String index,Integer type)
    {
        QueryWrapper<WorkZoneEntity> queryWrapper = new QueryWrapper<>();

        if ( index != null && !index.isEmpty()   &&   type != null ) {
            queryWrapper.like("region_index", index);
            queryWrapper.like("region_type", type);
        }
        else if (type != null) {
            queryWrapper.like("region_type", type);
        }
        else if (index != null && !index.isEmpty()) {
            queryWrapper.like("region_index", index);
        }

        List<WorkZoneEntity> workzoneCondition = workZoneMapper.selectList(queryWrapper);
        ResponseEntity responseEntity;
        if (!CollectionUtils.isEmpty(workzoneCondition)) {
            responseEntity = ResponseEntity.ok(MicroResponseUtils.success(workzoneCondition));
            return responseEntity;
        } else {
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }
    ///////////////////////// 工作区Work Zone的增删改查、分页  上 //////////////////////////////////////

    // 安全间隔字符串处理函数
    public static Integer[][][] Stringto3Array(String str) {
        String[] StringArray = str.split(",");      // 按“，”分割字符串
        Integer[][] SafeIntervalGroup = new Integer[3][10];           // 将分割后的字符串赋值给数组

        for (int i = 0; i < 3; i++) { // 遍历二维数组的行
            for (int j = 0; j < 10; j++) { // 遍历二维数组的列
                SafeIntervalGroup[i][j] = Integer.parseInt(StringArray[i * 10 + j]); // 计算索引并赋值
            }
        }
        // 定义三类安全间隔 最终的输出形式
        Integer[][][] safeinterval_output = new Integer[3][4][4];
        for (int i = 0; i < 3; i++) {
            safeinterval_output[i][0][0]=SafeIntervalGroup[i][0];  safeinterval_output[i][0][1]=SafeIntervalGroup[i][1];
            safeinterval_output[i][0][2]=SafeIntervalGroup[i][2];  safeinterval_output[i][0][3]=SafeIntervalGroup[i][3];
            safeinterval_output[i][1][0]=SafeIntervalGroup[i][1];  safeinterval_output[i][1][1]=SafeIntervalGroup[i][4];
            safeinterval_output[i][1][2]=SafeIntervalGroup[i][5];  safeinterval_output[i][1][3]=SafeIntervalGroup[i][6];
            safeinterval_output[i][2][0]=SafeIntervalGroup[i][2];  safeinterval_output[i][2][1]=SafeIntervalGroup[i][5];
            safeinterval_output[i][2][2]=SafeIntervalGroup[i][7];  safeinterval_output[i][2][3]=SafeIntervalGroup[i][8];
            safeinterval_output[i][3][0]=SafeIntervalGroup[i][3];  safeinterval_output[i][3][1]=SafeIntervalGroup[i][6];
            safeinterval_output[i][3][2]=SafeIntervalGroup[i][8];  safeinterval_output[i][3][3]=SafeIntervalGroup[i][9];
        }

        return safeinterval_output;
    }





}
